package com.kuangstudy.controller.blog;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuangstudy.entity.Blog;
import com.kuangstudy.service.blog.IBlogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Optional;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/11/12 20:19
 */
@Controller
@Slf4j
@RequiredArgsConstructor
public class BlogController {

    private final IBlogService blogService;

    @GetMapping("/blog/add")
    public ModelAndView toAdd() {
        // 3: 定义模型视图
        ModelAndView modelAndView = new ModelAndView();
        // 2: 指定跳转的页面
        modelAndView.setViewName("/blog/add");
        return modelAndView;
    }
    /**
     * 编辑
     *
     * @param blogId
     * @return
     */
    @GetMapping("/blog/edit/{blogId}")
    public ModelAndView toEdit(@PathVariable("blogId") Integer blogId) {
        // 1: 定义模型视图
        ModelAndView modelAndView = new ModelAndView();
        // 2: 把编辑的博客id放入到模型中
        modelAndView.addObject("blogId", blogId);
        // 2: 指定跳转的页面
        modelAndView.setViewName("/blog/add");
        return modelAndView;
    }


    /**
     * 根据id查询文章信息
     *
     * @param blogId
     * @return
     */
    @GetMapping("/api/blog/get/{blogId}")
    @ResponseBody
    public Blog geBlog(@PathVariable("blogId") Integer blogId) {
        Optional.ofNullable(blogId).orElseThrow(() -> new RuntimeException("文章找不到!!!"));
        Blog blog = blogService.getById(blogId);
        Optional.ofNullable(blog).orElseThrow(() -> new RuntimeException("文章找不到!!!"));
        return blog;
    }

    /**
     * 新增和添加文章接口
     *
     * @param blog
     * @return
     */
    @PostMapping("/api/blog/saveupdate")
    @ResponseBody
    public Blog saveBlog(@RequestBody Blog blog) {
        log.info("你保存的信息是：{}",blog);//cid = null
        return blogService.saveBlog(blog);
    }

    /**
     * 查询博客内容信息
     *
     * @return
     */
    @GetMapping("/api/blog/load")
    @ResponseBody
    public Page<Blog> findBlogs(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                @RequestParam(required = false, defaultValue = "0") Integer cid,
                                @RequestParam(required = false, defaultValue = "0") Integer sortnum) {
        // 1:设置分页起始和每页显示多少条
        Page page = new Page<>(pageNo, pageSize);
        // 2:设置条件
        LambdaQueryWrapper<Blog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 3：查询 status= 1 发布并且 isdelete=0未删除， 根据创建时间排序 desc
        lambdaQueryWrapper
                // 如果用户不传递cid或者传递的是0代表查询的全部
                .eq(cid != null && !cid.equals(0), Blog::getCategoryId, cid)
                .eq(Blog::getStatus, 1)
                .eq(Blog::getIsDelete, 0);
        if (sortnum.equals(0) || sortnum.equals(2)) {
            lambdaQueryWrapper.orderByDesc(Blog::getCreateTime);
        } else if (sortnum.equals(1)) {
            lambdaQueryWrapper.orderByAsc(Blog::getCreateTime);
        } else if (sortnum.equals(3)) {
            //自己根据条件去查询排序即可
        }
        // 4: 执行分页
        Page blogPage = blogService.page(page, lambdaQueryWrapper);
        // 5: 返回分页
        return blogPage;
    }


}
